package com.example.gokchinesefoodmapmcdev.controller;

import com.example.gokchinesefoodmapmcdev.entity.dto.OrderSubmitDTO;
import com.example.gokchinesefoodmapmcdev.service.MemberCartService;
import com.example.gokchinesefoodmapmcdev.service.OrderService;
import com.example.gokchinesefoodmapmcdev.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest/order")
public class OrderController {
    
    @Autowired
    private MemberCartService memberCartService;


    /**
     * 从购物车确认订单
     *      生成订单信息，未支付
     * ids   购物车项id数组
     * @return
     */
    @PostMapping("/cart/confirmOrder")
    public ResultData cartConfirmOrder(Integer[] ids){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        ResultData result = memberCartService.confirmOrderFromCart(ids, userDetails.getUsername());
        ResultData resultData = new ResultData();
        resultData.put("code", ResultData.OPERATION_SUCCESS);
        resultData.put("rows", result);
        return resultData;
    }

    /**
     * 提交订单
     * orderInfo  前端传入部分订单信息（收货地址id、商户id、配送方式id）
     * merchantOrders    以商户为单位报错商品的数组
     * goods             商品数组
     * goodSkuId         商品库id
     * number            商品数量
     * isCartOrder       是否为购物车订单（0否 1是），如果是，则删除购物车中提交商品
     * orderSource       订单来源（wx、app）
     * @return
     */
    @PostMapping("/submitOrder")
    public ResultData submitOrder(@RequestBody OrderSubmitDTO orderSubmitDTO){
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        ResultData resultData = memberCartService.submitOrderInfo(orderSubmitDTO, userDetails.getUsername());

        return resultData;
    }

}
